home *** CD-ROM | disk | FTP | other *** search
- Imports System.Runtime.InteropServices
-
- Module MainModule
-
- Sub Main()
- ' Run one of the Textxxxx procedures below by uncommenting only one statement
-
- 'TestFieldOffset()
- 'TestUnions()
- 'TestDllImport()
- 'TestConditionalAttribute()
- 'TestObsoleteAttribute()
- 'TestListVersioningAttributes()
-
- ' These statements are usuful when running inside Visual Studio.NET
- Console.WriteLine("")
- Console.WriteLine(">>> write Enter to terminate the program <<<")
- Console.ReadLine()
- End Sub
-
- ' This procedure tests the FieldOffset attributes
- Sub TestFieldOffset()
- ' Split a color into its components.
- Dim rgb As ARGBColor
- rgb.Value = &H112233 ' This is equal to 1122867.
- Console.WriteLine(String.Format("Red={0}, Green={1}, Blue={2}", _
- rgb.Red, rgb.Green, rgb.Blue))
- ' => Red=51, Green=34, Blue=17
- End Sub
-
- ' this procedure tests "unions"
-
- Sub TestUnions()
- Dim it As IntegerTypes
-
- it.Short0 = 517 ' hex 0205
- Console.WriteLine(it.Byte0) ' => 5
- Console.WriteLine(it.Byte1) ' => 2
-
- Console.WriteLine(it.LowByte(517)) ' => 5
- Console.WriteLine(it.HighByte(517)) ' => 2
- Console.WriteLine(it.LowWord(&HFFFF1000)) ' => 4096
- Console.WriteLine(it.HighWord(&HFFFF1000)) ' => -1
- End Sub
-
- ' Note that this code works even though the actual procedure
- ' is named FindWindowA, because the compiler tracks it automatically.
- <DllImport("user32")> _
- Function FindWindow _
- (ByVal lpClassName As String, ByVal lpWindowName As String) As Integer
- ' no implementation code
- End Function
-
- <DllImport("user32")> _
- Function MoveWindow _
- (ByVal hWnd As Integer, ByVal x As Integer, ByVal y As Integer, _
- ByVal nWidth As Integer, ByVal nHeight As Integer, _
- ByVal bRepaint As Integer) As Integer
- ' no implementation code
- End Function
-
- ' this procedure tests the DllImport attribute
-
- Sub TestDllImport()
- ' NOTE: launch Notepad on an empty document before running this code.
- Dim hWnd As Integer = FindWindow(Nothing, "Untitled - Notepad")
- ' If found, resize it and move it to upper-left corner.
- If hWnd <> 0 Then MoveWindow(hWnd, 0, 0, 600, 300, 1)
- End Sub
-
- ' this procedure tests the Conditional attribute
-
- Sub TestConditionalAttribute()
- LogMsg("Program is starting")
- ' ..
- LogMsg("Program is ending")
- End Sub
-
- ' Goto to the Configuration Properties | Build to set the LOG constant
- ' to a non-zero value to include this procedure
-
- <Conditional("LOG")> _
- Sub LogMsg(ByRef MsgText As String)
- Console.WriteLine(MsgText)
- End Sub
-
- ' this procedure tests the Obsolete attribute
-
- Sub TestObsoleteAttribute()
- Dim arr() As String
- ' next line is flagged as a warning
- BubbleSort(arr)
-
- ' *** uncomment next line to see a compile error
- ' BubbleSort2(arr)
- End Sub
-
- ' Mark the BubbleSort as obsolete - this causes a compiler warning
- <Obsolete("Replace BubbleSort with ShellSort")> _
- Sub BubbleSort(ByVal arr() As String)
- ' ...
- End Sub
-
- ' Mark the BubbleSort2 as obsolete - this causes a compiler error
- <Obsolete("Replace BubbleSort with ShellSort", True)> _
- Sub BubbleSort2(ByVal arr() As String)
- ' ...
- End Sub
-
- ' this procedure lists the Versioning attribute
- Sub TestListVersioningAttributes()
- Dim attributes() As Object
- Dim att As VersioningAttribute
- Dim method As System.Reflection.MethodInfo
- Dim attType As Type = GetType(VersioningAttribute)
- Dim classType As Type = GetType(TestClass)
-
- #Const ASSUME_MULTIPLE_OCCURRENCES = True
-
- #If ASSUME_MULTIPLE_OCCURRENCES Then
- ' this version shows how to query for custom attributes that
- ' can have multiple occurrences
-
- ' Retrieve all the custom attributes of type VersioningAttribute.
- attributes = Attribute.GetCustomAttributes(classType, attType)
-
- ' Check whether the array contains any element (it should contain one).
- If attributes.Length > 0 Then
- ' Move to a specific type variable so we can use early binding.
- att = DirectCast(attributes(0), VersioningAttribute)
- ' Display versioning information on the TestClass itself.
- Console.WriteLine("Class TestClass")
- Console.WriteLine(" Author = " & att.Author)
- Console.WriteLine(" Version = " & att.Version.ToString)
- Console.WriteLine(" Tested = " & att.Tested.ToString)
- End If
-
- ' Iterate over all the methods in TestClass.
- For Each method In classType.GetMethods
- ' Get Versioning attributes for this method.
- attributes = Attribute.GetCustomAttributes(method, attType)
- ' If there are custom Versioning attributes.
- If attributes.Length > 0 Then
- ' Display the name of this method.
- Console.WriteLine("Method " & method.Name)
- ' Get a Versioning object to use early binding.
- att = DirectCast(attributes(0), VersioningAttribute)
- ' Display versioning information on this method.
- Console.WriteLine(" Author = " & att.Author)
- Console.WriteLine(" Version = " & att.Version.ToString)
- Console.WriteLine(" Tested = " & att.Tested.ToString)
- End If
- Next
- #Else
-
- ' this simpler version assumes that the attribute has no
- ' multiple occurrences.
-
- ' Retrieve the (only) VersioningAttribute, or Nothing.
- att = DirectCast(Attribute.GetCustomAttribute(classType, attType), VersioningAttribute)
-
- ' Check whether the attribute was found.
- If Not (att Is Nothing) Then
- ' Display versioning information on the TestClass itself.
- Console.WriteLine("Class TestClass")
- Console.WriteLine(" Author = " & att.Author)
- Console.WriteLine(" Version = " & att.Version.ToString)
- Console.WriteLine(" Tested = " & att.Tested.ToString)
- End If
-
- ' Iterate over all the methods in TestClass.
- For Each method In classType.GetMethods
- ' Get the Versioning attribute for this method.
- att = DirectCast(Attribute.GetCustomAttribute(method, attType), VersioningAttribute)
- ' If there are custom Versioning attributes.
- If Not (att Is Nothing) Then
- ' Display the name of this method.
- Console.WriteLine("Method " & method.Name)
- ' Display versioning information on this method.
- Console.WriteLine(" Author = " & att.Author)
- Console.WriteLine(" Version = " & att.Version.ToString)
- Console.WriteLine(" Tested = " & att.Tested.ToString)
- End If
- Next
- #End If
-
- End Sub
-
- End Module
-